<script type="text/javascript">
$(function(){
	var pg = App.base.fileVersionIndex.ui, pp = pg.params
		, nv = pg.children('.navi'), hd = pg.children('.head')
		, bd = pg.children('.body'), gd = pg.seek('grid')
		, envs = Cache.base.env(App.code);

	nv.seek('back').click(pg.callback);
	nv.seek('title').text('文件版本管理 - ' + pp.app.code + pp.deployPath);
	
	var frm = {
		env: hd.seek('env'),
		key: hd.seek('key')
	}
	frm.env.mac('combo', {
		width: 70,
		boxWidth: 71,
		keyField: 'k',
		displayField: 'v',
		data: envs
	}).val('');
	
	gd.mac('grid', {
		cols : [{
			field: 'env', title: '环境', width: 80
		}, {
			field: 'ver', title : '版本号', width: 160,
			render: function(r){
				r.appId = pp.appId;
				return $('<span class="a"></span>').click(function(){
					App.open({
						title: '编辑文件版本',
						ctx: 'base',
						action: 'fileVersionEdit',
						params: r
					}, gd.load);
				}).append(r.ver);
			}
		}, {
			field: 'group', title : '分组', width: 120
		}, {
			field: 'lastTime', title : '修改时间', width: 160
		}, {
			field: 'memo', title : '备注', width: 240
		}, {
			field: 'status', title : '状态', width: 60,
			render: function(r){
				return Cache.base.file.version.status[r.status];
			}
		}, {
			field: 'id', title : '操作', width: 160,
			render: function(r){
				var a = $('<div></div>');
				$('<span class="a">发布</span>').click(function(){
					App.open({
						title: '请选择本次发布的目标实例',
						ctx: 'base',
						action: 'appInstPicker',
						params: {
							appId: pp.appId,
							env: frm.env.val(),
							multi: 1
						}
					}, function(sl){
						sl = $.map(sl, function(s){
							return s.addr;
						}).join(',');
						mac.confirm('确定要发布 ' + r.ver + ' 版本到'
							+ (sl? '指定实例':'所有实例') + '吗?', function () {
							var w = mac.wait('正在发布，请稍候...');
							$.post(App.ctx + '/file/version/push.do', {
								id: r.id,
								appId: pp.appId,
								fileId: r.fileId,
								key: sl
							}, function(rs){
								w.close();
								var ro = mac.eval(rs);
								if (ro.success) {
									App.open({
										ctx: 'base',
										action: 'fileVersionResult',
										model: true,
										params: ro.data
									}, function(){
										gd.load();
									});
								} else {
									mac.alert('推送失败');
								}
							});
						});
					});
				}).appendTo(a);
				a.append('&nbsp;&nbsp;');
				$('<span class="a">激活</span>').click(function(){
					mac.confirm('确定要激活 ' + r.ver + ' 版本？', function () {
						$.post(App.ctx + '/file/version/active.do', {
							id: r.id,
							appId: pp.appId,
							fileId: r.fileId
						}, function(rs){
							var ro = mac.eval(rs);
							if (ro.success) {
								mac.alert('激活成功');
							} else {
								mac.alert('激活失败');
							}
							gd.load();
						});
					});
				}).appendTo(a);
				a.append('&nbsp;&nbsp;');
				//
				var f = $('<form method="post" action="' + App.ctx
						+ '/file/version/download.do" class="hidden"></form>');
					f.append('<input type="hidden" name="id" value="' + r.id + '" />');
					f.append('<input type="hidden" name="appId" value="' + pp.appId + '" />');
					f.append('<input type="hidden" name="key" value="' + pp.name + '" />');
				a.append(f);
				//
				$('<span class="a">下载</span>').click(function(){
					f[0].submit();
				}).appendTo(a);
				a.append('&nbsp;&nbsp;');
				$('<span class="a">删除</span>').click(function(){
					mac.confirm('确定要 删除 ' + r.ver + ' 版本？', function () {
						$.post(App.ctx + '/file/version/delete.do', {
							id: r.id,
							appId: pp.appId,
							fileId: r.fileId
						}, function(){
							gd.load();	
						});
					});
				}).appendTo(a);
				return a;
			}
		}],
		loader: {
			url: App.ctx + '/file/version/list.do',
			params: {
				appId: pp.app.id, fileId: pp.id,
				pageNo: 1, pageSize: 50
			},
			autoLoad: true
		},
		pagerLength: 10
	});
	pg.seek('add').click(function(){
		App.open({
			title: '添加新版本',
			ctx: 'base',
			action: 'fileVersionEdit',
			model: true,
			params: {
				appId: pp.app.id,
				fileId: pp.id,
				env: frm.env.val()
			}
		}, function(){
			gd.load()
		});
	});
	function find(e){
		gd.load({ env: frm.env.val(),  key: frm.key.val() });
	}
	frm.key.keydown(function(e) {
		if (e.keyCode == 13) {
			find(e);
		}
	});
	hd.seek('find').click(find);
	
	function adjust(){
		var h = pg.parent().height();
		bd.height(h - hd.height() - nv.height() - 24);
		gd.adjust();
	}
	$(window).resize(adjust);
	adjust();
});
</script>
<div class="navi">
	<span class="a" name="back">返回</span>&gt;&gt;
	<span name="title">文件版本管理</span>
</div>
<div class="head">
	<span class="fl">环境：</span>
	<div class="combo fl" name="env"></div>
	<div class="fl">
		&nbsp;&nbsp;&nbsp;&nbsp;
		<button name="add">添加版本</button>
	</div>
	<div class="fl">
		&nbsp;&nbsp;&nbsp;&nbsp;关键字： 
		<input type="text" name="key" placeholder="查找" />
		<button name="find">查找</button>
	</div>
	<div class="clear"></div>
</div>
<div class="clear"></div>
<div class="body">
	<div class="grid" name="grid"></div>
</div>